﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class LocationRepo
    {
        public const string m_msg_head = "ProcedureRemark";

        public IEnumerable<LocationModels> GetLocation()
        {
            List<LocationModels> Locations = new List<LocationModels>();
            string SQLCommand = @"select * from TBLocation";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        LocationModels Location = new LocationModels();
                        Location.LocationId = (string)reader["LocationId"];
                        Location.Description = (string)reader["Description"];
                        Location.Status = (string)reader["Status"];
                        Location.CreateBy = (string)reader["CreateBy"];
                        Location.CreateOn = (DateTime)reader["CreateOn"];
                        Location.UpdateBy = (string)reader["UpdateBy"];
                        Location.UpdateOn = (DateTime)reader["UpdateOn"];

                        Locations.Add(Location);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return Locations.ToList();
        }

        public LocationModels GetLocationById(string id)
        {
            LocationModels Location = new LocationModels();
            string SQLCommand = @"select * from TBLocation where LocationId=@LocationId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@LocationId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        Location.LocationId = (string)reader["LocationId"];
                        Location.Description = (string)reader["Description"];
                        Location.Status = (string)reader["Status"];
                        Location.CreateBy = (string)reader["CreateBy"];
                        Location.CreateOn = (DateTime)reader["CreateOn"];
                        Location.UpdateBy = (string)reader["UpdateBy"];
                        Location.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return Location;
        }

        public bool Create(LocationModels Location)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBLocation
                                    values (@LocationId,@ProcedureRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@LocationId", "CallGenId");
                    db.AddParameter("@Description", Location.Description);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(LocationModels Location)
        {
            bool result = true;
            string SQLCommand = @"Update TBLocation 
                                    Set Description=@Description,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where LocationId=@LocationId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@LocationId", Location.LocationId);
                    db.AddParameter("@Description", Location.Description);
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(LocationModels Location)
        {
            bool result = true;
            string SQLCommand = @"Update TBLocation
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where LocationId=@LocationId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@LocationId", Location.LocationId);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}